define(['validate'],function (validate) {
    $.validator.setDefaults({
        highlight: function (element) {
            $(element).closest('.form-group').removeClass('has-success').addClass('has-error');
        },
        success: function (element) {
            element.closest('.form-group').removeClass('has-error').addClass('has-success');
            $(".form-group.has-success input").removeClass("help-block")
            $(".form-group.has-success span").removeClass("help-block")
        },
        errorElement: "span",
        errorPlacement: function (error, element) {
            if (element.is(":radio") || element.is(":checkbox")) {
                error.appendTo(element.parent().parent().parent());
            } else {
                error.appendTo(element.parent());
            }
        },
        errorClass: "help-block"
    });
    /*验证中文*/
    $.validator.addMethod("chinese", function(value, element) {
        var chinese = /^[\u4e00-\u9fa5]+$/;
        return this.optional(element) || (chinese.test(value));
    }, "只能输入中文");

    /*验证数字*/
    $.validator.addMethod("number", function(value, element) {
        var number = /^[0-9]*$/;
        return this.optional(element) || (number.test(value));
    }, "只能输入数字");

    /*验证大、小写字母*/
    $.validator.addMethod("english", function(value, element) {
        var english = /^[a-zA-Z]*$/;
        return this.optional(element) || (english.test(value));
    }, "只能输入英文字母");

    /*验证手机号*/
    $.validator.addMethod("telephone", function(value, element) {
        var telephone = /^1(3|4|5|7|8)\d{9}$/;
        return this.optional(element) || (telephone.test(value));
    }, "请输入有效的手机号");

    /*验证座机号码*/
    $.validator.addMethod("phone", function(value, element) {
        var phone = /^(\(\d{3,4}\)|\d{3,4}-|\s)?\d{7,14}$/;
        return this.optional(element) || (phone.test(value));
    }, "请输入有效的电话号码");

    /*验证身份证号码15位和18位*/
    $.validator.addMethod("idCard", function(value, element) {
        var idCard = /^\d{6}(18|19|20)?\d{2}(0[1-9]|1[012])(0[1-9]|[12]\d|3[01])\d{3}(\d|[xX])$/;
        return this.optional(element) || (idCard.test(value));
    }, "请输入有效的身份证号");

    /*验证邮箱*/
    $.validator.addMethod("email", function(value, element) {
        var email = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
        return this.optional(element) || (email.test(value));
    }, "请输入有效的邮箱地址");
    return{
        validate:function($dom,rules,message,callback){
            $dom.validate({
                rules: rules,
                messages: message,
                submitHandler:callback
            });
        },
        ResetForm:function($dom){
            $dom.validate().resetForm();
            $dom.find('.form-group').removeClass('has-error has-success');
        },
        serializeObject:function(dom) {
            var o = {};
            var a = dom.serializeArray();
            $.each(a, function() {
                if (o[this.name] !== undefined) {
                    if (!o[this.name].push) {
                        o[this.name] = [ o[this.name] ];
                    }
                    o[this.name].push(this.value || '');
                } else {
                    o[this.name] = this.value || '';
                }
            });
            return o;
        }
    };
})